package com.lovo.controller;

import com.github.pagehelper.PageInfo;
import com.lovo.model.Service;
import com.lovo.service.ServiceService;
import com.lovo.service.impl.ServiceServiceImpl;
import com.lovo.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ServiceServlet",urlPatterns = "/service.do")
public class ServiceServlet extends HttpServlet {
    ServiceService ss = new ServiceServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if (method == null || method.equals("")){
            indexByPage(request,response);
        }else if (method.equals("addService")){
            addService(request,response);
        }else if (method.equals("deleteService")){
            deleteService(request,response);
        }else if (method.equals("updateService")){
            updateService(request,response);
        }
    }

    private void updateService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        Integer serviceId = Integer.parseInt(request.getParameter("serviceId"));
        String serviceName = request.getParameter("serviceName");
        String serviceType = request.getParameter("serviceType");
        String serviceWeightStandard = request.getParameter("serviceWeightStandard");
        String serviceTime = request.getParameter("serviceTime");
        String serviceDate = request.getParameter("serviceDate");
        String serviceStandard = request.getParameter("serviceStandard");
        String serviceLevel = request.getParameter("serviceLevel");
        Double servicePrice = Double.parseDouble(request.getParameter("servicePrice"));

        Service s = new Service(serviceId,serviceName,serviceType,serviceDate,serviceTime,serviceWeightStandard,serviceStandard,serviceLevel,servicePrice);
        boolean flag = ss.update(s);
        out.write(String.valueOf(flag));
    }

    private void deleteService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int serviceId = Integer.parseInt(request.getParameter("serviceId"));
        boolean flag = ss.delete(serviceId);
        out.write(String.valueOf(flag));
    }

    private void addService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String serviceName = request.getParameter("serviceName");
        String serviceType = request.getParameter("serviceType");
        String serviceWeightStandard = request.getParameter("serviceWeightStandard");
        String serviceTime = request.getParameter("serviceTime");
        String serviceDate = request.getParameter("serviceDate");
        String serviceStandard = request.getParameter("serviceStandard");
        String serviceLevel = request.getParameter("serviceLevel");
        Double servicePrice = Double.parseDouble(request.getParameter("servicePrice"));

        Service s = new Service(serviceName,serviceType,serviceDate,serviceTime,serviceWeightStandard,serviceStandard,serviceLevel,servicePrice);
        boolean flag = ss.insert(s);
        out.write(String.valueOf(flag));
    }

    private void indexByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int pageNo = 1;
        int pageSize = 10;
        String pageNo1 = request.getParameter("pageNo");
        String pageSize1 = request.getParameter("pageSize");
        if(pageNo1!=null&&!pageNo1.equals("")){
            pageNo = Integer.parseInt(pageNo1);
        }
        if(pageSize1!=null&&!pageSize1.equals("")){
            pageSize = Integer.parseInt(pageSize1);
        }

        String serviceName = request.getParameter("serviceName");
        String serviceType = request.getParameter("serviceType");
        String serviceDate = request.getParameter("serviceDate");
        PageInfo<Service>servicePageInfo = ss.listByPage(pageNo,pageSize,serviceName,serviceType,serviceDate);
        out.write(MyUtils.toJSON(servicePageInfo));
    }
}
